/**
 * 
 */
package com.edu.lawdoc.i18n;

import java.io.UnsupportedEncodingException;

import com.edu.lawdoc.env.Enviroment;
import com.edu.lawdoc.errors.Errors;
import com.jfinal.i18n.I18n;
import com.jfinal.i18n.Res;

/**
 * @author hu.xiao
 *
 */
public class I18nDelegate {

	// 通过locale参数en_US得到对应的Res对象 Res resEn = I18n.use("en_US");
	static Res res = I18n.use(Enviroment.i18nLocal);
	
	public static boolean contains(String msgKey) {
		return res.getResourceBundle().containsKey(msgKey);
	}
	
	public static String get(String msgKey) {
		/**
		 * 必须要判断配置文件中是否存在key，不然会抛出异常
		 */
		if(!contains(msgKey)) return msgKey;
		
		// 直接获取数据
		String msgVal = res.get(msgKey);
		
		try {
			return new String(msgVal.getBytes("ISO-8859-1"), "UTF8");
		} catch (UnsupportedEncodingException e) {
			return msgVal;
		}
	}
	
	public static String get(Errors enumError) {
		String msgKey = enumError.name();
		return get(msgKey);
	}
	
	public static String get(String msgKey, Object ...str) {
		String msgVal = res.format(msgKey, str);
		try {
			return new String(msgVal.getBytes("ISO-8859-1"), "UTF8");
		} catch (UnsupportedEncodingException e) {
			return msgVal;
		}
	}
}
